<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

use App\Eloquent\Area;
use App\Eloquent\City;
use App\Eloquent\Province;

class LogisticsLine extends DbEloquent{

    protected $table    = 'ygt_logistics_line';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'logistics_id','title'=>'物流公司','value'=>0,'is_must'=>1],
        ['name'=>'title','title'=>'专线类型','value'=>'','is_must'=>1],
        ['name'=>'contact_name','title'=>'联系人','value'=>'','is_must'=>1],
        ['name'=>'contact_mobile','title'=>'联系电话','value'=>'','is_must'=>0],
        ['name'=>'province_id','title'=>'省','value'=>0,'is_must'=>0],
        ['name'=>'city_id','title'=>'市','value'=>0,'is_must'=>0],
        ['name'=>'area_id','title'=>'县','value'=>0,'is_must'=>0],
        ['name'=>'address','title'=>'详细地址','value'=>'','is_must'=>0],
    ];

    public static function edit($data,$pk='')
    {
        $id                 = isset($data['id']) ? intval($data['id']) : 0;
        $provinceId         = $data['province_id'];
        $cityId             = $data['city_id'];
        $areaId             = $data['area_id'];
        $provinceName       = $cityName = $areaName = '';
        if($provinceId != 0)
        {
            $where          = ['provinceID'=>$provinceId];
            $collection     = Province::where($where)->first();
            $provinceName   = $collection->province;
        }
        if($cityId != 0)
        {
            $where          = ['cityID'=>$cityId];
            $collection     = City::where($where)->first();
            $cityName       = $collection->city;
        }
        if($areaId != 0)
        {
            $where          = ['areaID'=>$areaId];
            $collection     = Area::where($where)->first();
            $areaName       = $collection->area;
        }
        $data['province_name']  = $provinceName;
        $data['city_name']      = $cityName;
        $data['area_name']      = $areaName;
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where,$data,$pk);
        } else {
            $result         = self::insertOneData($data,$pk);
        }
        return $result;
    }
}
